﻿using BCCommon;
using BCData.Common.Dictionary;
using BCData.OA.TeamWorks.AttendanceTaskRecord;
using BCDto.OA.Workflow.WorkflowActivityInstance;
using BCDto.OA.Workflow.WorkflowOperation;
using BCEntity.Common.Dictionary;
using BCEntity.OA.TeamWorks.TaskRecord;
using BCService.OA.Workflow.AttendanceBusiness;
using BCService.OA.Workflow.WorkflowActivityInstanceBusiness;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BCService.OA.Workflow.OAWorkflowFactoryInstance
{
    /// <summary>
    /// 考勤工作流相关服务
    /// </summary>
    public class AttendanceWorkflowFactoryInstanceService : IAttendanceWorkflowFactoryInstanceService
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly IAttendanceTaskRecordData attendanceTaskRecordData;
        private readonly IDictionaryReferenceData dictionaryReferenceData;
        private readonly IWorkflowActivityInstanceBusinessService workflowActivityInstanceBusinessService;
        private readonly IRedisService redisService;

        public AttendanceWorkflowFactoryInstanceService(IApplicationContextService applicationContextService,
            IAttendanceTaskRecordData attendanceTaskRecordData,
            IDictionaryReferenceData dictionaryReferenceData,
            IWorkflowActivityInstanceBusinessService workflowActivityInstanceBusinessService,
            IRedisService redisService)
        {
            this.applicationContextService = applicationContextService;
            this.attendanceTaskRecordData = attendanceTaskRecordData;
            this.dictionaryReferenceData = dictionaryReferenceData;
            this.workflowActivityInstanceBusinessService = workflowActivityInstanceBusinessService;
            this.redisService = redisService;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Closed(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            attendanceFactoryService.Closed(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        public bool InitActivityStepInstanceAssign(long activityInstanceId)
        {
            var instanceDto = GetActivityInstanceEntity(activityInstanceId);
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            return attendanceFactoryService.InitActivityStepInstanceAssign(instanceDto);
        }

        /// <summary>
        /// 初始化指派服务
        /// </summary>
        /// <param name="instanceDto"></param>
        /// <returns></returns>
        public bool InitAssign(WorkflowActivityInstanceDto instanceDto)
        {
            //创建补卡申请工作流实例
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            return attendanceFactoryService.InitAssign(instanceDto);
        }


        /// <summary>
        /// 根据配置不同，获取不同的指派考勤业务
        /// </summary>
        /// <param name="instanceDto"></param>
        /// <returns></returns>
        private IAttendanceBusinessFactoryService GetAssignService(WorkflowActivityInstanceDto instanceDto)
        {
            DictionaryReferenceEntity dictionaryReferenceEntity = this.redisService.TryGet(instanceDto.WorkflowKey, () =>
            {
                return this.dictionaryReferenceData.GetEntityAsync(instanceDto.WorkflowKey).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));

            if (dictionaryReferenceEntity == null)
            {
                throw new ArgumentException("字典中缺少工作流Key，工作流Key:" + instanceDto.WorkflowKey);
            }

            IAttendanceBusinessFactoryService attendanceBusinessFactoryService;
            switch (dictionaryReferenceEntity.Name)
            {
                case "RemedyCheckIn":
                    attendanceBusinessFactoryService = applicationContextService.RequestServices.GetService<IRemedyCheckInWorkflowService>();
                    break;
                default:
                    attendanceBusinessFactoryService = null;
                    break;
            }
            return attendanceBusinessFactoryService;
        }

        /// <summary>
        /// 通过
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Pass(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            attendanceFactoryService.Pass(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Paused(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            attendanceFactoryService.Paused(instanceDto, taskRecordEntity, workflowOperationRequestDto);
        }

        /// <summary>
        /// 拒绝
        /// </summary>
        /// <param name="workflowOperationRequestDto"></param>
        public void Reject(WorkflowOperationRequestDto workflowOperationRequestDto)
        {
            var taskRecordEntity = GetTaskRecordEntity(workflowOperationRequestDto.TaskRecordNo);
            var instanceDto = GetActivityInstanceEntity(taskRecordEntity.WorkflowActivityInstanceId);
            IAttendanceBusinessFactoryService attendanceFactoryService = this.GetAssignService(instanceDto);
            attendanceFactoryService.Reject(instanceDto, taskRecordEntity, workflowOperationRequestDto);

        }

        #region 获取活动实例信息
        /// <summary>
        /// 获取活动实例信息(带步骤实例列表)
        /// </summary>
        /// <param name="activityInstanceId">活动实例Id</param>
        /// <returns></returns>
        private WorkflowActivityInstanceDto GetActivityInstanceEntity(long activityInstanceId)
        {
            return this.workflowActivityInstanceBusinessService.GetInstanceInfo(activityInstanceId);
        }
        #endregion

        #region 获取任务记录信息
        private TaskRecordEntity GetTaskRecordEntity(string taskRecordNo)
        {
            var taskRecordEntity = this.attendanceTaskRecordData.GetByTaskRecordNo(taskRecordNo).GetAwaiter().GetResult();
            if (taskRecordEntity == null)
            {
                throw new ArgumentException("此任务编号不存在");
            }
            return taskRecordEntity;
        }
        #endregion


    }
}
